<template>
    <el-dialog
            :title="!dataForm.projectId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="项目ID，主键" prop="projectId">
                        <el-input v-model.number="dataForm.projectId" placeholder="项目ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="项目名称" prop="projectName">
                        <el-input v-model="dataForm.projectName" placeholder="项目名称"></el-input>
                </el-form-item>
                <el-form-item label="项目编号" prop="projectCode">
                        <el-input v-model="dataForm.projectCode" placeholder="项目编号"></el-input>
                </el-form-item>
                <el-form-item label="项目负责人" prop="projectManager">
                        <el-input v-model="dataForm.projectManager" placeholder="项目负责人"></el-input>
                </el-form-item>
                <el-form-item label="项目开始日期" prop="startDate">
                        <el-date-picker
                                v-model="dataForm.startDate"
                                type="datetime"
                                placeholder="项目开始日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="项目结束日期" prop="endDate">
                        <el-date-picker
                                v-model="dataForm.endDate"
                                type="datetime"
                                placeholder="项目结束日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="项目预算" prop="budget">
                        <el-input v-model="dataForm.budget" placeholder="项目预算"></el-input>
                </el-form-item>
                <el-form-item label="项目类型" prop="projectType">
                        <el-input v-model="dataForm.projectType" placeholder="项目类型"></el-input>
                </el-form-item>
                <el-form-item label="项目优先级" prop="projectPriority">
                        <el-input v-model.number="dataForm.projectPriority" placeholder="项目优先级"></el-input>
                </el-form-item>
                <el-form-item label="记录创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="记录创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="记录更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="记录更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            projectId: null,
            projectName: null,
            projectCode: null,
            projectManager: null,
            startDate: null,
            endDate: null,
            budget: null,
            projectType: null,
            projectPriority: null,
            createdAt: null,
            updatedAt: null
          },
          dataRule: {
            projectId: [
                        { required: true, message: '项目ID，主键不能为空', trigger: 'blur' }
            ],
            projectName: [
                        { required: true, message: '项目名称不能为空', trigger: 'blur' }
            ],
            projectCode: [
                        { required: true, message: '项目编号不能为空', trigger: 'blur' }
            ],
            projectManager: [
                        { required: true, message: '项目负责人不能为空', trigger: 'blur' }
            ],
            startDate: [
                        { required: true, message: '项目开始日期不能为空', trigger: 'blur' }
            ],
            endDate: [
                        { required: true, message: '项目结束日期不能为空', trigger: 'blur' }
            ],
            budget: [
                        { required: true, message: '项目预算不能为空', trigger: 'blur' }
            ],
            projectType: [
                        { required: true, message: '项目类型不能为空', trigger: 'blur' }
            ],
            projectPriority: [
                        { required: true, message: '项目优先级不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.projectId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.projectId = id || 0
            if (this.dataForm.projectId) {
              this.$http({
                url: this.$http.adornUrl(`/app/project_info/info/${this.dataForm.projectId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appprojectinfo
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/project_info/${!this.dataForm.projectId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>